package ezXpns.GUI;

import java.util.Stack;


/**
 * @author A0086931X
 */

//@author A0086931X
public class UndoManager {	
	public void execute(UndoableAction action) {
		action.execute();
		redoStack.clear();
		undoStack.push(action);
	}
	
	public void undo() {
		if(canUndo()) {
			UndoableAction action = undoStack.pop();
			action.undo();
			redoStack.push(action);
		}
	}
	
	public void redo() {
		if(canRedo()) {
			UndoableAction action = redoStack.pop();
			action.execute();
			undoStack.push(action);
		}
	}
	
	public boolean canUndo() {
		return !undoStack.isEmpty();
	}
	
	public boolean canRedo() {
		return !redoStack.isEmpty();
	}
	
	public String getNextUndoActionName() {
		return undoStack.lastElement().getName();
	}
	
	public String getNextRedoActionName() {
		return redoStack.lastElement().getName();
	}
	
	private Stack<UndoableAction> undoStack = new Stack<UndoableAction>();
	private Stack<UndoableAction> redoStack = new Stack<UndoableAction>();
}
